import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class flterJapaneseWord {

	/**
	 * 这个程序的作用是删除关键词列表里的日语关键词，扫描keywords数据库，含有日文字符的行的 flter字段会修改为0，flter原值是1
	 */
	public static void main(String[] args) {

		KeyWordFilterParameter kfp = new KeyWordFilterParameter();
		// 两个list里装载了过滤删除和分流正则
		List<String> splitList = kfp.getSplitRegList();
		List<String> delList = kfp.getDelRegList();
		String host = kfp.getHost();
		String dBName = kfp.getDbName();
		Connection conn = null;
		Statement stmt = null;
		Statement stmtUpdate = null;
		ResultSet rs = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://" + host + "/" + dBName + "?user=root");
			stmt = conn.createStatement();
			stmtUpdate = conn.createStatement();
//是否打开一次性非重复
			//rs = stmt.executeQuery("select id, keyword from keywordnofilter where filter = '2011-03-17 17:10:04'");
			rs = stmt.executeQuery("select id, keyword from keywordnofilter");
			while (rs.next()) {
				String kw = rs.getString("keyword");
				// 检索需要删除的部分
				boolean flag = false;
				for (int i = 0; i < delList.size(); i++) {
					Pattern p = Pattern.compile(delList.get(i));
					Matcher m = p.matcher(kw);
					if (m.find()) {
						// 需要删除的在filter位置0
						// stmtUpdate.executeUpdate("update keywords set filter = 0 where id = "
						// + rs.getInt("id"));
						flag = true;
						break;
					}
				}
				// 检索出来del关键词就把delTag标记位修改为1
				if (flag) {
					// stmtUpdate.execute("insert into keywords (keyword,kwEng,gbk) values ('"
					// + kw + "','" + URLEncoder.encode(kw, "UTF-8") + "','" +
					// URLEncoder.encode(kw, "gbk") + "')");
					stmtUpdate.executeUpdate("update keywordnofilter set delTag = 1 where id = " + rs.getInt(1));
				}
				// 检索需要删除的部分
				for (int i = 0; i < splitList.size(); i++) {
					Pattern p = Pattern.compile(splitList.get(i));
					Matcher m = p.matcher(kw);
					// 如果匹配到那么修改相对应的键值
					if (m.find()) {
						stmtUpdate.executeUpdate("update keywordnofilter set second" + i + " = 1  where id = " + rs.getInt(1));
					}
				}
			}
			// 2011-03-17 17:10:04就是一个默认值，默认是用全零的查询有问题
			stmtUpdate.execute("update keywordnofilter set filter = now() where filter = '2011-03-17 17:10:04'");
			// System.out.println(postContent);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (stmt != null) {
					stmt.close();
					stmt = null;
				}
				if (conn != null) {
					conn.close();
					conn = null;
				}
				if (stmtUpdate != null) {
					stmtUpdate.close();
					stmtUpdate = null;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}

}
